SMTPSocket Class

Used to send email via the SMTP protocol.

Events

ConnectionEstablished

Error

MailSent

MessageSent

SendComplete

SendProgress

ServerError


Properties

Messages


Methods

DeleteAllMessages

DisconnectFromServer

SendMail


More information available in parent classes: TCPSocket:SocketCore:Object


Notes

The POP3Socket and SMTPSocket classes are used together to form the basis of an email client. POP3 is the standard internet protocol for receiving messages and SMTP (Simple Mail Transfer Protocol) is the standard internet protocol for sending emails.

SMTPSocket supports the 'login' authentication type as well as 'plain.' This increases the authentication compatibility with some servers.

If you use a constructor in a subclass of SMTPSocket, you must call the Super class's constructor in your subclass's constructor. The subclass will not work unless this is done.


Example

The following code in the Action event of a PushButton sends an email message. It works with a form with fields for each part of the email. EditFields are used for the Username and Password of the email account, the From, To, and CC addresses, the subject of the email, and the body in plain text and HTML form. Since the user can enter multiple To and CC addresses, the example parses theses strings and adds the individual To and CC addresses to the EmailMessage using the AddRecipient and AddCCRecipient methods.

An attachment can be specified in the form of a full path to the file to be attached. If an attachment exists, it is added to the EmailMessage's Attachments property.

An SMTPSocket named SMTPSocket1, has been added to the window.

Dim mail as EmailMessage
Dim file as EmailAttachment
Dim i as Integer
Dim s as String
  
SMTPSocket1.address = "mail.mySMTPServer.com"
SMTPSocket1.port = 25
  
SMTPSocket1.username = usernameFld.text  //get username from editfield
SMTPSocket1.password = passwordFld.text  //get password from Editfield

mail = New EmailMessage
mail.fromAddress=fromAddressFld.text  //get From address from EditField
mail.subject=subjectFld.text  //get Subject of mail from EditField
mail.bodyPlainText = bodyFld.text   //get body in plain text from EditField
mail.bodyHTML = htmlFld.text  //get body in HTML from EditField
mail.headers.appendHeader "X-Mailer","REALbasic SMTP Demo"
  
//multiple To addresses separated by commas
//each to address added to Recipient array
s = ReplaceAll(toAddressFld.text,",", Chr(13))
For i = 1 to CountFields(s, Chr(13))
 mail.addRecipient Trim( NthField(s, Chr(13),i))
next

//multiple CC addresses separated by commas
//each to address added to CCRecipient array

s = ReplaceAll(ccAddressFld.text,",", Chr(13))
For i = 1 to CountFields(s, Chr(13))
 mail.addCCRecipient Trim( NthField(s, Chr(13),i))
next
//add attachments, if any
If fileFld.text <> "" then //is there a path to an attachment file?
 file = New EmailAttachment
 file.loadFromFile GetFolderItem(fileFld.text)
 mail.attachments.append file //add file to attachments array
end
  //send the mail
SMTPSocket1.messages.append mail //add email to list of messages
SMTPSocket1.SendMail //send message

See Also

EmailAttachment, EmailHeader, EmailMessage, HTTPSecureSocket, HTTPSocket, POP3SecureSocket, POP3Socket, SMTPSecureSocket, SocketCore, TCPSocket classes.